              package com.java.diagnostics.visualizer.impl.data.ids;
              
              import com.java.diagnostics.visualizer.data.ids.ID;
              import com.java.diagnostics.visualizer.impl.factory.LogFactory;
              import java.text.MessageFormat;
              import java.util.logging.Logger;
              import java.util.regex.Matcher;
              import java.util.regex.Pattern;
              
              public abstract class IDImpl
                implements ID
              {
                private static final Logger TRACE = LogFactory.getTrace(IDImpl.class);
                private static final String TEMPLATE_STRING = "Label: {0} Category: {1} ({2})";
                private static final MessageFormat template = new MessageFormat("Label: {0} Category: {1} ({2})");
                private static final String REGEX = "Label: {0} Category: {1} ({2})".replaceAll("\\(", "\\\\(").replaceAll("\\)", "\\\\)").replaceAll("\\{[0-9]\\}", "(\\.\\*)");
                private static final Pattern PATTERN = Pattern.compile(REGEX);
                protected String string;
              
                protected IDImpl(String category, String descriptor)
                {
                  this.string = template.format(new Object[] { descriptor, category, getKind() });
                }
              
                protected abstract String getKind();
              
                public int hashCode()
                {
                  return this.string.hashCode();
                }
              
                public boolean equals(Object obj)
                {
                  if (this == obj) {
                    return true;
                  }
              
                  if ((obj instanceof IDImpl)) {
                    return this.string.equals(((IDImpl)obj).string);
                  }
                  return false;
                }
              
                public String toString()
                {
                  return this.string;
                }
              
                public static IDImpl fromString(String string)
                {
                  Matcher matcher = PATTERN.matcher(string);
                  IDImpl id = null;
                  if (matcher.find())
                  {
                    String label = matcher.group(1);
                    String category = matcher.group(2);
                    String type = matcher.group(3);
                    if ("Tuple".equals(type))
                    {
                      id = new TupleIDImpl(category, label);
                    } else if ("Structured".equals(type))
                    {
                      id = new StructuredIDImpl(category, label);
                    }
                    else if ("Unstructured".equals(type))
                    {
                      id = new UnstructuredIDImpl(category, label);
                    }
              
                  }
              
                  if (id == null)
                  {
                    TRACE.warning("Could not construct an ID from string " + string);
                  }
              
                  return id;
                }
              
                public int compareTo(Object id)
                {
                  return toString().compareTo(id.toString());
                }
              }

